Imports CapaDeNegocios
Partial Public Class FrmImputacionesABm
    Inherits System.Web.UI.Page
    Dim Imputacion As CapaDeNegocios.nImputacion = New CapaDeNegocios.nImputacion
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            LLenarGridImputaciones()
            LblError.Visible = False
        End If

    End Sub


    Protected Sub GridImputacion_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridImputacion.RowCommand
        If e.CommandName = "Select" Then
            TxtDescripcion.Text = e.CommandSource.parent.parent.FindControl("LblDescripcion").text
            CboSaldo.SelectedValue = e.CommandSource.parent.parent.FindControl("Lblsaldo").text
            HFCodigoImp.Value = e.CommandSource.parent.parent.FindControl("HFCodigoImputacion").value
        End If
    End Sub
    Sub LLenarGridImputaciones()
        Dim Descripcion As String
        Dim Saldo As String
        If TxtDescripcion.Text = "" Then
            Descripcion = ""
        Else
            Descripcion = Trim(TxtDescripcion.Text)
        End If
        If CboSaldo.Text = "Ninguno" Then
            saldo = ""
        Else
            Saldo = CboSaldo.SelectedValue.ToString()
        End If
        GridImputacion.DataSource = Imputacion.ListarImputacion(0, Descripcion, Saldo)
        GridImputacion.DataBind()
    End Sub
    Protected Sub LinkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkGuardar.Click
        LblError.Visible = False
        If HFCodigoImp.Value = "" Then
            If Len(Trim(TxtDescripcion.Text)) > 0 And CboSaldo.Text <> "Ninguno" Then
                If (Imputacion.InsertarImputacion(Trim(TxtDescripcion.Text), CboSaldo.Text)) Then
                    Me.Response.Write("<script languaje='javascript'>window.alert('El Alta se realizo Correctamente')</script>")
                    LimpiarFormulario()
                Else
                    LblError.Text = "Error, no se pudo realizar el Alta"
                    LblError.Visible = True
                End If
            Else
                LblError.Text = "Error, los datos no son correctos"
                LblError.Visible = True
            End If
        Else
            If Len(Trim(TxtDescripcion.Text)) > 0 And CboSaldo.Text <> "Ninguno" Then
                If (Imputacion.ModificarImputacion(Int(HFCodigoImp.Value), Trim(TxtDescripcion.Text), CboSaldo.Text)) Then
                    Me.Response.Write("<script languaje='javascript'>window.alert('La Modificacion fue Correcta')</script>")
                    LimpiarFormulario()
                Else
                    LblError.Text = "Error, no se pudo realizar la modificacion"
                    LblError.Visible = True
                End If
            Else
                LblError.Text = "Error, los datos no son correctos"
                LblError.Visible = True
            End If
        End If
    End Sub

    Protected Sub LinkEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkEliminar.Click
        LblError.Visible = False
        If HFCodigoImp.Value <> "" Then
            If Imputacion.EliminarImputacion(HFCodigoImp.Value) Then
                LblError.Visible = True
                LblError.Text = "Error,Los datos se eliminaron Correctamente!"
                LimpiarFormulario()
            Else
                LblError.Visible = True
                LblError.Text = "Error,No se pudo realizar la eliminacion!"
            End If
        Else
            LblError.Visible = True
            LblError.Text = "Error,No selecciono elementos!"
        End If

    End Sub
    Sub LimpiarFormulario()
        TxtDescripcion.Text = ""
        CboSaldo.SelectedIndex = -1
        GridImputacion.SelectedIndex = -1
        LblError.Visible = False
        HFCodigoImp.Value = ""
        LLenarGridImputaciones()
    End Sub
    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        LimpiarFormulario()
    End Sub

    Protected Sub LinkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkBuscar.Click
        LLenarGridImputaciones()
    End Sub

    Protected Sub GridImputacion_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridImputacion.PageIndexChanging
        GridImputacion.PageIndex = e.NewPageIndex
        LLenarGridImputaciones()
    End Sub
End Class